import { languages } from "@/api";

export const rating = (number) => {
  if (number.toString().length <= 1) {
    return `${number.toString()}.0`;
  }
  return number.toString();
};

export const numberWithCommas = (num) => {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};

export const numberWithDoubleDigits = (num) => {
  if (num < 10) {
    return `0${num}`;
  }
  return num;
};

export const runtime = (minutes) => {
  const seconds = minutes * 60;
  let secondsLeft = seconds;

  // hours
  const hours = Math.floor(secondsLeft / 3600);
  secondsLeft = secondsLeft % 3600;

  // mins
  const mins = Math.floor(secondsLeft / 60);
  secondsLeft = secondsLeft % 60;

  return `${hours ? hours + "h" : ""} ${mins}min`;
};

export const truncate = (text, length, clamp) => {
  text = text || "";
  clamp = clamp || "...";
  length = length || 30;

  if (text.length <= length) return text;

  let tcText = text.slice(0, length - clamp.length);
  let last = tcText.length - 1;

  while (last > 0 && tcText[last] !== " " && tcText[last] !== clamp[0])
    last -= 1;

  // Fix for case when text dont have any `space`
  last = last || length - clamp.length;

  tcText = tcText.slice(0, last);

  return tcText + clamp;
};

export const arrayToList = (array) => {
  return array.map((item) => item.name).join(", ");
};

export const fullDate = (string) => {
  const dateArray = string.split("-");
  const date =
    dateArray[2].substr(0, 1) === "0"
      ? dateArray[2].substr(1, 1)
      : dateArray[2];
  const months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
  ];
  return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`;
};

export const fullLang = (iso) => {
  const fullLang = languages.find((lang) => lang.iso_639_1 === iso);
  if (fullLang) {
    return fullLang.english_name;
  }
  return iso;
};
